package google.apps.reporting.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.GregorianCalendar;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;


public class WorkBookGenerator {

  public WorkBookGenerator() {
  }

  public static String generateWorkBook(Object[][] data, String[] columns) {
    FileOutputStream out = null;
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    String filePath = "";
    try {


      File f =
        File.createTempFile("report_" + GregorianCalendar.getInstance().toString(),
                            ".xls");
      out = new FileOutputStream(f);
      filePath = f.getAbsolutePath();
      HSSFRow row = s.createRow(0);
      row.setHeightInPoints((float)12.75);
      HSSFCell cell = null;
      HSSFCellStyle style = wb.createCellStyle();
      style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
      style.setFillBackgroundColor((short)0xC);

      HSSFFont f2 = wb.createFont();

      f2.setFontHeightInPoints((short)10);
      //make it red
      f2.setColor((short)HSSFFont.COLOR_RED);
      //make it bold
      f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);


      style.setFont(f2);

      //create columns headers
      for (int i = 0; i < columns.length; i++) {
        cell = row.createCell((short)i);
        cell.setCellValue(columns[i]);
        cell.setCellStyle(style);

      }

      //create data
      for (int i = 0; i < data.length; i++) {
        row = s.createRow(i + 1);
        row.setHeightInPoints((float)12.75);

        for (int j = 0; j < data[0].length; j++) {
          cell = row.createCell((short)j);
          cell.setCellValue((String)data[i][j]);
        }
      }


    } catch (FileNotFoundException e) {

      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        wb.write(out);
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }

    }

    return filePath;
  }

  public static String generateWorkBookFromCSVData(BufferedReader reader,
                                                   String directory) {
    FileOutputStream out = null;
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    String filePath = "";
    try {
      File f = null;
      if (directory == null) {
        f = File.createTempFile("/report_" + System.nanoTime(), ".xls");
      } else {
        f = new File(directory + "/report_" + System.nanoTime() + ".xls");
      }
      out = new FileOutputStream(f);
      filePath = f.getAbsolutePath();
      HSSFRow row = s.createRow(0);
      row.setHeightInPoints((float)12.75);
      HSSFCell cell = null;
      HSSFCellStyle style = wb.createCellStyle();
      style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
      style.setFillBackgroundColor((short)0xC);

      HSSFFont f2 = wb.createFont();

      f2.setFontHeightInPoints((short)10);
      //make it red
      f2.setColor((short)HSSFFont.COLOR_RED);
      //make it bold
      f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);


      style.setFont(f2);

      //read headers from File
      String[] columns = reader.readLine().split(",");
      int columnCount = columns.length;
      //create columns headers
      for (int i = 0; i < columnCount; i++) {
        cell = row.createCell((short)i);
        cell.setCellValue(columns[i]);
        cell.setCellStyle(style);

      }

      String currentRecord = null;

      int rowCount = 1;
      String[] rowData = null;

      while ((currentRecord = reader.readLine()) != null) {
        row = s.createRow(rowCount++);
        row.setHeightInPoints((float)12.75);
        rowData = currentRecord.split(",");

        for (int j = 0; j < columnCount; j++) {
          cell = row.createCell((short)j);
          cell.setCellValue((String)rowData[j]);
        }
      }

    } catch (FileNotFoundException e) {

      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        wb.write(out);
        out.close();
      } catch (IOException e) {
        e.printStackTrace();
      }

    }

    return filePath;
  }


}
